package org.example.book.service.impl;

import org.example.book.bean.Book;
import org.example.book.conception.BizException;
import org.example.book.conception.BizExceptionEnum;
import org.example.book.mapper.BookMapper;
import org.example.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 猪头
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public void update(Book book) {
        Integer id = book.getId();
        if (id == null) {
            throw new BizException(BizExceptionEnum.USER_NOT_EXIST);
        }
        Book bookINdb = bookMapper.queryBookById(id);
        if (bookINdb == null) {
            throw new BizException(BizExceptionEnum.USER_NOT_EXIST);
        }
        if (StringUtils.hasText(book.getName())) {
            bookINdb.setName(book.getName());
        }
        if (StringUtils.hasText(book.getAuthor())) {
            bookINdb.setAuthor(book.getAuthor());
        }
        if (book.getPrice() != null) {
            bookINdb.setPrice(book.getPrice());
        }
        if (book.getStock() != null) {
            bookINdb.setStock(book.getStock());
        }
        bookMapper.updateBook(bookINdb);

    }
    // 在 BookServiceImpl.java 中添加以下方法实现
@Override
public List<Book> queryBooksByPriceRange(Double minPrice, Double maxPrice) {
    return bookMapper.queryBooksByPriceRange(minPrice, maxPrice);
}


    @Override
    public void add(Book book) {
        bookMapper.addBook(book);
    }

    @Override
    public void delete(Integer id) {
        bookMapper.deleteBookById(id);
    }

    @Override
    public Book queryById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    @Override
    public List<Book> queryAll() {
        return bookMapper.queryAllBooks();
    }
}
